%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This is part of the set of files that accompany the article:       %
%Mankiw, N. Gregory and Ricardo Reis (2007) "Sticky Information in  %
%General Equilibrium," Journal of the European Economic Association,%
%forthcoming. See the appendix of the NBER or CEPR working paper    %
%versions for a detailed explanation of the algorithms.             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Please cite if you use the programs. I do not provide tech support.%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Last revised: August 30, 2006                                      %
%Written by: Ricardo Reis                                           %
%Input: the Bayesdraw#.mat files                                    %
%Output: Convergencec diagnostics and joint draws matrix Bayes.mat  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear; clc

%%%%% STEP 1: LOAD FILES %%%%%%
load Bayesdraw1;
FinalDraws=draws; FinalAccepts=accepts; FinalLogds=logds;
load Bayesdraw2;
FinalDraws=[FinalDraws draws]; FinalAccepts=[FinalAccepts accepts];
FinalLogds=[FinalLogds lods];
load Bayesdraw3;
FinalDraws=[FinalDraws draws]; FinalAccepts=[FinalAccepts accepts];
FinalLogds=[FinalLogds lods];
load Bayesdraw4;
FinalDraws=[FinalDraws draws]; FinalAccepts=[FinalAccepts accepts];
FinalLogds=[FinalLogds lods];
load Bayesdraw5;
FinalDraws=[FinalDraws draws]; FinalAccepts=[FinalAccepts accepts];
FinalLogds=[FinalLogds lods];
clear K N T Y accept accepts bat beta burnin data draws f gamma i logds
clear nu phipi phiy psi rho_a whi_g rho_gam rho_m rho_nu sigma_a sigma_g
clear sigma_nu sigma_m sigma_gam theta zxc rho_g M

%%%%% STEP 2: DISPLAYING RESULTS AND DETERMINING BURN-IN %%%%%%
disp('Calibrated parameters:');
disp('    beta      psi       theta    nu        gamma      phiy      phipi')
disp(pars_c(:,1:7));
disp('    rho_m     sigma_m   rho_a     sigma_a');
disp(pars_c(:,8:11));
disp('    rho_g     sigma_g   rho_nu    sigma_nu  rho_gam   sigma_gam');
disp(pars_c(:,12:17));
disp('delta omega lambda');
disp(pars_c(:,18:20));
disp(' ')
disp('Total number of draws and number of independent samples:');
[M m]=size(FinalAccepts); disp([M m]);
burnin=30000; disp('Burn-in draws:'); disp(burnin);
accept_rate = mean(sum(FinalAccepts(burnin+1:M,:))./(M-burnin));
disp('Accept rates:'); disp(accept_rate);
results = FinalDraws(burnin+1:M,:);

%%%%% STEP 3: ASSESSING CONVERGENCE OF CHAIN %%%%%%
[Ri Vi Wi]=SIGEconverge(results,20,kk); R=Ri(:,20);
disp('Scale factors'); disp(R); disp(' ');
disp('Look at graph to assess convergence. Press key to proceed...')
disp('')
pause

%%%%% STEP 4: MIX THE DRAWS AND SAVE %%%%%%
MixedResults=[];
for i=1:m
    MixedResults=[MixedResults; results(:,kk*(i-1)+1:kk*i)];
end
clear Pmean Ppar Pvar VARCOV Ri Vi Wi VARCOV i kk M m xo FinalDraws
clear FinalLogds FinalAccepts results x0
save Bayes.mat
